package qf.controller;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package qf.controller
 * @Description:
 * @Date 2022/5/27 12:50
 */
public class aa extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String methodStr = req.getParameter("method");
        System.out.println("===========方法名" + methodStr);
        if (methodStr == null) {
            resp.sendRedirect("/index.jsp");
        }
        Class clazz = this.getClass();
        try {
            Method method = clazz.getMethod(methodStr, HttpServletRequest.class, HttpServletResponse.class);
            System.out.println("===========方法" + method);
            Object results  = method.invoke(this, req, resp);
            if (results != null) {
                String jump = (String)results;
                if (jump.startsWith("forward:")) {
                    String path = jump.substring(jump.indexOf(":")+1);
                    req.getRequestDispatcher(path);
                }else if (jump.startsWith("redirect:")){
                    String path = jump.substring(jump.indexOf(":")+1);
                    resp.sendRedirect(path);
                }else {
                resp.getWriter().println(jump);
            }
            }


        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }
}
